

//
//  BAHeaderNode.m
//  BabyAlbum
//
//  Created by master on 17/2/15.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BANormalHeaderNode.h"
#import "BAResource.h"
@interface BANormalHeaderNode ()

@property (nonatomic, strong) ASImageNode *coverNode;
@property (nonatomic, strong) ASTextNode *titleNode;

@property (nonatomic, strong) ASTextNode *dateNode;
@property (nonatomic, strong) ASTextNode *weekNode;
//@property (nonatomic, strong) ASDisplayNode *lineNode;
@property (nonatomic, strong) ASTextNode *detailNode;

@property (nonatomic, strong) BAAlbumModel *model;


@end

@implementation BANormalHeaderNode

- (instancetype)initWithData:(BAAlbumModel *)model ;
{
    if(self = [super init]) {

        self.model = model;
        _coverNode = [[ASImageNode alloc] init];
        _coverNode.image = [model.cover coverImage] ? [model.cover coverImage] : kDefaultCoverImage;
        [_coverNode addTarget:self action:@selector(clickCover) forControlEvents:ASControlNodeEventTouchUpInside];
        [self addSubnode:_coverNode];
        
        _titleNode = [[ASTextNode alloc] init];
        NSAttributedString *string =[NSAttributedString attributedStringWithString:model.title fontSize:18 color:RGBWhite(44)];
        _titleNode.attributedText = string;
        [self addSubnode:_titleNode];
        

        _dateNode = [[ASTextNode alloc] init];
        string =[NSAttributedString attributedStringWithString:[model customDateShow] fontSize:13 color:RGB_255(188,197,195)];
        _dateNode.attributedText = string;
        [self addSubnode:_dateNode];
        
        
        _weekNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:[[model weekDayShow] length]?[model weekDayShow]:[[NSDate date] weekDayShow] fontSize:13 color:RGB_255(188,197,195)];
        _weekNode.attributedText = string;
        [self addSubnode:_weekNode];


        _detailNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:model.desc fontSize:11 color:RGBWhite(140)];
        _detailNode.attributedText = string;
        [self addSubnode:_detailNode];
        
    }
    return self;
}

- (void)clickCover {
    if(self.tapBlock) {
        self.tapBlock();
    }
}

- (UIImage *)image {
    return _coverNode.image;
}


- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _coverNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.width);
    _dateNode.style.flexShrink  = YES;
    _titleNode.style.flexShrink  = NO;
//    _titleNode.style.maxSize = CGSizeMake(constrainedSize.max.width, 30);
    _dateNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 60, 20);

    _detailNode.style.flexShrink = YES;
    _detailNode.style.minSize = CGSizeMake(kScreenWidth - 20, 80);
  
    
    ASRelativeLayoutSpec *dateSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_dateNode];
    ASRelativeLayoutSpec *weekSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionEnd verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_weekNode];
    
    ASStackLayoutSpec *dataHor = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsCenter children:@[dateSpec,weekSpec]];

    
    ASStackLayoutSpec *ver1 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_coverNode,_titleNode,dataHor,_detailNode]];
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:ver1];
    
    
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:insetLayout];
}

+ (CGFloat)calculteHeightWithData:(BAAlbumModel *)albumModel {
//    CGFloat descHeight = [albumModel.desc uxy_calculateSize:CGSizeMake(kScreenWidth - 30, CGFLOAT_MAX) font:[UIFont systemFontOfSize:11]].height;
    CGFloat descHeight = [UILabel getHeightByWidth:kScreenWidth - 20 title:albumModel.desc font:[UIFont systemFontOfSize:11]];
    CGFloat titleHeight = [UILabel getHeightByWidth:kScreenWidth - 20 title:albumModel.title font:[UIFont systemFontOfSize:18]];

    if(descHeight < 10) {
        descHeight = 80;
    }
    CGFloat height = 5 + kScreenWidth - 20 + 5 + 5 + descHeight + 5 + titleHeight + 5 + 10;
    return height;
    
}

- (UIImage *)cover {
    return _coverNode.image;
}

@end
